import os
from pyglet import image
from pyglet import media
from pyglet import gl
from math import ceil

def load_image(file_name):
	directory = os.path.abspath(os.path.dirname(__file__))
	full_path = os.path.join(directory, 'data', file_name)
	return image.load(full_path)

def load_music(file_name, **kwargs):
	directory = os.path.abspath(os.path.dirname(__file__))
	full_path = os.path.join(directory, 'data', file_name)
	return media.load(full_path, **kwargs)

class Map():
	
	def __init__(self):
		# loads the logical map into an image
		self.logic_image = load_image("logic_map.bmp")
		# converts the logical map into a string ("\x00" = ground, "\xff" = clear)
		self.logic_data = self.logic_image.get_data("I", self.logic_image.width)
		# loads the artistic map
		print("load nice map!")
		self.nice_map = load_image("nice_map.jpg")
		self.nice_parts = []
		# split the image
		print("split nice map!")
		for x in range(0, int(ceil(self.nice_map.width / 100))):
			self.nice_parts.append(self.nice_map.get_region(x * 800, 0, 800, 600).get_image_data())
		print("nice map done!")
	
	def is_ground(self, x, y):
		# the logic map i represented as 10x10 px blocks
		x = int(x / 10)
		y = int(y / 10)
		if (y < 0 or y > self.logic_image.height - 1 or x < 0 or x > self.logic_image.width - 1):
			return True
		return self.logic_data[y * self.logic_image.width + x] == "\x00"
	
	def draw(self, offset):
		# draw the two visible images
		self.nice_parts[(int) (offset/800)].blit(-(offset%800), 0)
		self.nice_parts[int((offset/800) + 1)].blit(800-(offset%800) , 0)
		
		gl.glColor4f(1, 1, 1, 0.4)
		self.logic_image.blit(-offset, 0, 0, self.nice_map.width, 600)
		gl.glColor4f(1, 1, 1, 1)
	
	def update(self, dt):
		pass
	
	def get_width(self):
		return self.nice_map.width

map = Map()

def limit(test, limit):
	if (test > limit):
		return limit
	return test

class Game():
	bird = 0
	buffalo = 0
	bear = 0
	bird_feather = load_image("bird_feather.png")
	buffalo_feather = load_image("buffalo_feather.png")
	bear_feather = load_image("bear_feather.png")
	feathers = []
	sprites = []
	victory = False
	game_over = False

def draw_stats():
	x = 10
	y = 570
	for feather in Game.feathers:
		feather.blit(x, y)
		x += feather.width - 12
	

def update_stats(dt):
	Game.bird -= dt
	Game.buffalo -= dt
	Game.bear -= dt

def kill_bird():
	Game.feathers.append(Game.bird_feather)
	if Game.bird < 0:
		Game.bird = 3
	else:
		Game.bird += 3
	if (Game.bird > 8):
		Game.bird = 8

def kill_buffalo():
	Game.feathers.append(Game.buffalo_feather)
	if Game.buffalo < 0:
		Game.buffalo = 3
	else:
		Game.buffalo += 3
	if (Game.buffalo > 8):
		Game.buffalo = 8

def kill_bear():
	Game.feathers.append(Game.bear_feather)
	if Game.bear < 0:
		Game.bear = 3
	else:
		Game.bear += 3
	if (Game.bear > 8):
		Game.bear = 8

def is_bird():
	if (Game.bird > 0):
		return True
	return False

def is_buffalo():
	if (Game.buffalo > 0):
		return True
	return False

def is_bear():
	if (Game.bear > 0):
		return True
	return False